package com.luoling.shell;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * Description:
 * Copyright:   Copyright (c)2024
 * Company:
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2024-05-29 17:35:14
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2024-05-29     张李鑫                     1.0         1.0 Version
 */

public class ZipShell extends Shell {
    private static final Logger log = LoggerFactory.getLogger(ZipShell.class);
    private final int defaultSize = 100;


    public ZipShell(HashMap<String, String> params) {
        super("zip", params);

    }

    @Override
    public boolean execute() throws Exception {

        // 创建 ProcessBuilder 对象
        Process process = this.start();


        // 等待进程执行完毕
        if (process.waitFor() == 0) {
            boolean result = true;
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), this.getFormat()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                // 在这里根据输出结果来进行判断
                result = result && !line.contains("zip error");
            }
            return result;
        }
        this.print();
        return false;
    }


    //zip -s 100m -j  ~/oracleDump/2024/BOOK/1717040513252/child/BOOK_2024.zip ~/oracleDump/2024/BOOK/1716963875553/BOOK_2024.dmp
    @Override
    public String buildCommand() {
        HashMap<String, String> params = getParams();
        String zipName = params.get("zipName");
        String pre = params.get("pre");

        return this.getCommandPrefix() +
                " -r  " +
                zipName + ".zip " +
                pre;
    }


}
